<?php

namespace app\api1\controller;

use app\common\lib\verify\VerifyFactory;
use app\common\model\MemberLoginLog;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;

/**
 * 会员
 *
 * api-Member
 *
 * @Apidoc\Title("会员")
 * @Apidoc\Group("api")
 */
class Member extends Auth
{
    /**
     * getInfo
     * @Apidoc\Title("获取会员信息")
     */
    public function getInfo(): \think\response\Json
    {
        $res = \app\common\model\Member::with(['memberInformationRobot'])->find($this->request->uid);
        return success('ok', $res);
    }




    /**
     * update
     * @return \think\response\Json
     * @Apidoc\Title("更新会员信息")
     */
    public function update()
    {
        $data = $this->request->post(['name', 'province', 'address']);
        validate(['name|昵称' => 'require'])->check($data);
        \app\common\model\Member::where('id', $this->request->uid)->save($data);
        return success();
    }

    /**
     * logout
     * @Apidoc\Title("退出登录")
     */
    public function logout(): \think\response\Json
    {
        $token = str_replace('Bearer ', '', $this->request->header('Authorization'));
        JWTAuth::invalidate($token);
        return success();
    }

    /**
     * loginLog
     * @Apidoc\Title("登录日志")
     */
    public function loginLog()
    {
        $list_rows = input('list_rows', 10, 'intval');
        $res = MemberLoginLog::where('member_id', $this->request->uid)->order('id desc')->paginate($list_rows);
        return success('', $res);
    }


    /**
     * checkEmailStep1
     * @Apidoc\Title("验证修改邮箱第一步")
     */
    public function checkEmailStep1()
    {
        $data = input();
        validate([
            'phone|邮箱' => 'require',
            'code|验证码' => 'require'
        ])->check($data);
        VerifyFactory::verify($data['phone'], $data['code']);
        cache('email_step2_' . $this->request->uid, true, 600);
        return success();
    }

    /**
     * checkEmailStep2
     * @Apidoc\Title("验证修改邮箱第二步")
     */
    public function checkEmailStep2()
    {
        $data = input();
        validate([
            'email|邮箱' => 'require',
            'code|验证码' => 'require'
        ])->check($data);
        VerifyFactory::verify($data['email'], $data['code']);
        $member = \app\common\model\Member::find($this->request->uid);
        $member->email = $data['email'];
        $member->save();
        cache('email_step2_' . $this->request->uid, null);
        return success();
    }

    /**
     * canEmailStep2
     * @Apidoc\Title("能否修改邮箱第二步")
     */
    public function canEmailStep2()
    {
        $email = \app\common\model\Member::where('id', $this->request->uid)->value('email');
        return success('', (bool)cache('email_step2_' . $this->request->uid) || !$email);
    }



    /**
     * checkPhoneStep1
     * @Apidoc\Title("验证修改手机第一步")
     */
    public function checkPhoneStep1()
    {
        $data = input();
        validate([
            'phone|手机号码' => 'require',
            'code|验证码' => 'require'
        ])->check($data);
        VerifyFactory::verify($data['phone'], $data['code']);
        cache('phone_step2_' . $this->request->uid, true, 600);
        return success();
    }

    /**
     * checkPhoneStep2
     * @Apidoc\Title("验证修改手机第二步")
     */
    public function checkPhoneStep2()
    {
        $data = input();
        validate([
            'phone|手机号码' => 'require',
            'code|验证码' => 'require'
        ])->check($data);
        VerifyFactory::verify($data['phone'], $data['code']);
        $member = \app\common\model\Member::find($this->request->uid);
        $member->phone = $data['phone'];
        $member->save();
        cache('phone_step2_' . $this->request->uid, null);
        return success();
    }

    /**
     * canPhoneStep2
     * @Apidoc\Title("能否修改手机第二步")
     */
    public function canPhoneStep2()
    {
        return success('', (bool)cache('phone_step2_' . $this->request->uid));
    }



}
